<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="当前每日更新时间：">
        {{time.hour}}:{{time.minute}}:{{time.second}}
      </el-form-item>
      <el-form-item>
        <el-button v-if="isManager" type="primary" @click="reRefreshNew">立即获取</el-button>
        <el-button v-if="isManager"  type="primary" @click="openSetting">设置更新时间</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="原货币代码" prop="fromCurrency" min-width="180" align="center"></el-table-column>
        <el-table-column label="原货币名称" prop="fromCurrencyName" min-width="180" align="center"></el-table-column>
        <el-table-column label="转换货币代码" prop="toCurrency" min-width="180" align="center"></el-table-column>
        <el-table-column label="转换货币名称" prop="toCurrencyName" min-width="180" align="center"></el-table-column>
        <el-table-column label="汇率" prop="rate" min-width="180" align="center"></el-table-column>
        <el-table-column label="更新时间" prop="updateDate" min-width="180" align="center"></el-table-column>
      </el-table>
      <el-dialog title="汇率更新时间设置" :visible.sync="itemDialog" width="600px" :close-on-click-modal="false"> 
        <el-form :model="form" style="padding-right:100px;padding-left: 50px;" label-width="140px">
          <el-form-item label="时：">
            <el-input v-model="form.hour"></el-input>
          </el-form-item>
          <el-form-item label="分：">
            <el-input v-model="form.minute"></el-input>
          </el-form-item>
          <el-form-item label="秒：">
            <el-input v-model="form.second"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="itemDialog = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import formUtil from '@/utils/formUtil'
  import dingMixin from  '@/mixins/ding'
  import * as dd from 'dingtalk-jsapi'

  export default {
    data () {
      return {
        tableData: [],
        time: {},
        itemDialog:false,
        form: {
          hour: '',
          minute: '',
          second: ''
        }
      }
    },
    computed: {
      isManager() {
        return this.userInfo.roleIdList && this.userInfo.roleIdList.includes('1265175017173348354')
      }
    },
    mixins: [dingMixin],
    components:{

    },
    async mounted(){
      await this.searchItem();
      await this.getUpdateTime();
    },
    methods: {
      async searchItem() {
        this.tableData = await this.$httpN.postQ('mpb/exchange/list')
      },
      async reRefreshNew() {
        await this.$httpN.postQ('mpb/exchange/refreshAll');
        formUtil.submitSuccess('请求成功');
        this.tableData = await this.$httpN.postQ('mpb/exchange/list')
      },
      async getUpdateTime() {
        this.time = await this.$httpN.get('mpb/exchange/refreshTime')
      },
      openSetting() {
        this.form.hour = this.time.hour;
        this.form.minute = this.time.minute;
        this.form.second = this.time.second;

        this.itemDialog = true;
      },
      async save() {
        if(!await this.$httpN.postQ('mpb/exchange/setRefreshTime', this.form,false)) return;
        this.itemDialog = false;
        formUtil.submitSuccess('保存成功');
        this.getUpdateTime();
      }
    }
  }
</script>
